//
//  CloseButton.swift
//  SimpleCal
//
//  Created by 杜晓祥 on 2021/10/21.
//

import Cocoa

@IBDesignable
class CloseButton: BaseView {

    var image1View:NSImageView!
    var image2View:NSImageView!
    

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        initialize()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        initialize()
    }
    
    private func initialize () {
        image1View = NSImageView()
        image1View.frame = NSRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)
        image1View.image = NSImage(named: NSImage.statusUnavailableName)
        image1View.imageScaling = NSImageScaling.scaleProportionallyUpOrDown
        image1View.alphaValue = 0.7
        addSubview(image1View)
        image2View = NSImageView()
        image2View.frame = NSRect(x: 0, y: 0, width: self.frame.width * 0.6, height: self.frame.height * 0.6)
        image2View.image = NSImage(named: NSImage.stopProgressTemplateName)
        image2View.imageScaling = NSImageScaling.scaleProportionallyUpOrDown
        image2View.alphaValue = 0.9
        addSubview(image2View)
        image2View.isHidden = true
    }
    
    override func layout() {
        let h = self.frame.height
        let w = self.frame.width
        image1View.setFrameSize(NSSize(width: w, height: h))
       // image2View.setFrameSize(NSSize(width: self.frame.width * 0.7, height: self.frame.height * 0.7))
        let v:CGFloat = 0.4
        let x:CGFloat = (w - (w * v)) * 0.5
        let y:CGFloat = (h - (h * v)) * 0.5
        
        image2View.frame = NSRect(x: x, y: y, width: w * v, height: h * v)
    }
    
    override func mouseDown(with event: NSEvent) {
        image1View.alphaValue = 0.9
        NSApplication.shared.terminate(self)
    }
    
    override func mouseEntered(with event: NSEvent) {
        image1View.alphaValue = 0.8
        image2View.isHidden = false
    }
    
    override func mouseExited(with event: NSEvent) {
        image1View.alphaValue = 0.7
        image2View.isHidden = true
    }
    
}
